﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

using ERP_RORZE_DLL.Product.database;
using ERP_RORZE_DLL.Product;
using ERP_RORZE_DLL.Product.Base;

namespace ERP_RORZE_PRODUCT.Product.BusOder
{
    public partial class Frm_BusOderEdit : DevExpress.XtraEditors.XtraForm
    {
        ClsBusOderTable busoder = new ClsBusOderTable();
        ClsBusOderDAO clsBusOderDAO = new ClsBusOderDAO();
        public event EventHandler Re_Load;
        DataTable dt;

        public string BOderNo;
        public string date;
        public string planNo;
        public string PartID;
        public string Qty;
        public string Dealine;
        public string RawQty;
        public string HelisertQty;
        public string BlastQty;
        public string MONo;
        public string MOQty;
        public string Started;
        public string Finished;
        public string Change;
        public string ChangeDate;
        public string Imported;
        public string ImportFrom;
        public string FinishDate;
        public string Note;
        
        
        public Frm_BusOderEdit()
        {
            InitializeComponent();
        }
        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void Frm_BusOderEdit_Load(object sender, EventArgs e)
        {

            //MessageBox.Show("busOder: " + Imported + "busOder: " + Started + "busOder: " + Finished);
            // lấy danh sách PartID truyền vào cbPartID
            //dt = clsBusOderDAO.GetPartID();
            //cbPartID.DataSource = dt;
            //cbPartID.DisplayMember = "PartNo";
            //cbPartID.ValueMember = "PartNo";

            //Lấy danh sách MONo truyền vào cbMONo
            //dt = clsBusOderDAO.GetMONo();
            //cbMONo.DataSource = dt;
            //cbMONo.DisplayMember = "MONo";
            //cbMONo.ValueMember = "MONo";

            // điền thông tin của bản ghi vào các ô để sửa
            txtBlastQty.Text = BlastQty;
            txtBusOderNo.Text = BOderNo;
            txtChange.Text = Change;
            txtHelisertQty.Text = HelisertQty;
            txtImpordFrom.Text = ImportFrom;
            txtMOQty.Text = MOQty;
            txtNote.Text = Note;
            txtPlanNo.Text = planNo;
            txtQty.Text = Qty;
            txtRawQty.Text = RawQty;
            cbPartID.Text = PartID;
            
            dateChangeDate.Value = DateTime.Parse(ChangeDate);
            dateDeadLine.Value = DateTime.Parse(Dealine);
            dateDate.Value = DateTime.Parse(date);
            checkFinished.Checked = bool.Parse(Finished);
            checkStarted.Checked = bool.Parse(Started);
            checkImported.Checked = bool.Parse(Imported);
            dateFinishDate.Value = DateTime.Parse(FinishDate);

        }
        void ThongBao(string name)
        {
            MessageBox.Show("Chưa nhập " + name + ". Vui lòng nhập lại");
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            if (txtBusOderNo.Text == "")
            {
                ThongBao(clsConstantsdb.Entities.ClsDBBusOder.FieldName.BOderNo);
            }
            else if (txtBlastQty.Text == "")
            {
                ThongBao(clsConstantsdb.Entities.ClsDBBusOder.FieldName.BlastQty);
            }
            //else if (txtChange.Text == "")
            //{
            //    ThongBao(clsConstantsdb.Entities.ClsDBBusOder.FieldName.Change);
            //}
            else if (txtHelisertQty.Text == "")
            {
                ThongBao(clsConstantsdb.Entities.ClsDBBusOder.FieldName.HelisertQty);
            }
            //else if (txtImpordFrom.Text == "")
            //{
            //    ThongBao(clsConstantsdb.Entities.ClsDBBusOder.FieldName.ImportFrom);
            //}
            //else if (txtMOQty.Text == "")
            //{
            //    ThongBao(clsConstantsdb.Entities.ClsDBBusOder.FieldName.MOQty);
            //}
            //else if (txtNote.Text == "")
            //{
            //    ThongBao(clsConstantsdb.Entities.ClsDBBusOder.FieldName.Note);
            //}
            else if (txtPlanNo.Text == "")
            {
                ThongBao(clsConstantsdb.Entities.ClsDBBusOder.FieldName.planNo);
            }
            else if (txtQty.Text == "")
            {
                ThongBao(clsConstantsdb.Entities.ClsDBBusOder.FieldName.Qty);
            }
            else if (txtRawQty.Text == "")
            {
                ThongBao(clsConstantsdb.Entities.ClsDBBusOder.FieldName.RawQty);
            }
            else
            {
                try
                {
                    busoder.BOderNo = txtBusOderNo.Text;
                    busoder.Date = dateDate.Value;
                    busoder.Dealine = dateDeadLine.Value;
                    busoder.ChangeDate = dateChangeDate.Value;
                    busoder.BlastQty = int.Parse(txtBlastQty.Text);
                    busoder.Change = txtChange.Text;
                    busoder.Finished = checkFinished.Checked;
                    busoder.HelisertQty = int.Parse(txtHelisertQty.Text);
                    busoder.Imported = checkImported.Checked;
                    busoder.ImportFrom = txtImpordFrom.Text;
                    //busoder.MONo = cbMONo.Text;
                    //busoder.MOQty = int.Parse(txtMOQty.Text);
                    busoder.Note = txtNote.Text;
                    busoder.PartID = cbPartID.SelectedValue.ToString();
                    busoder.PlanNo = txtPlanNo.Text;
                    busoder.Qty = int.Parse(txtQty.Text);
                    busoder.RawQty = int.Parse(txtRawQty.Text);
                    busoder.Started = checkStarted.Checked;
                    busoder.FinishDate = dateFinishDate.Value;
                    clsBusOderDAO.Update(busoder);
                    MessageBox.Show("Đã cập nhật thành công");
                    this.Re_Load(sender, e);
                }
                catch
                {
                    MessageBox.Show("các trường RawQty, Qty, MOQty, HelisertQty, BlastQty");
                }
            }
        }

        private void txtMOQty_EditValueChanged(object sender, EventArgs e)
        {

        }
    }
}